﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using nanoCMS.Core;
using nanoCMS.StaticPages.Concrete;
using nanoCMS.StaticPages.Providers;
using nanoCMS.StaticPages.WebUI.Models.ViewModels;

namespace nanoCMS.StaticPages.WebUI.Models
{
    public class StaticPagesService
    {
        private readonly WorldContext worldContext;
        private readonly StaticPageProvider provider;

        public StaticPagesService(WorldContext worldContext)
        {
            this.worldContext = worldContext;
            provider = new StaticPageProvider(worldContext);
        }

        public StaticPageAdminVM CreateStaticPageIndexModel()
        {
            StaticPageAdminVM model = new StaticPageAdminVM()
            {
                Page = new StaticPageVM(),
                Pages = provider.GetAll().Select(VMFactory.ŞtaticPageVMFactory).ToList()
            };
            return model;
        }

        public void CreateNewPage(StaticPageVM page)
        {
            provider.Create(page.Title, page.Content);
        }

        public void EditPage(StaticPageVM page)
        {
            provider.Edit(page.PageID, page.Title, page.Content);
        }

        public void Delete(int pageID)
        {
            provider.Delete(pageID);
        }

        public StaticPageAdminVM CreateStaticPageEditModel(int pageID)
        {
            StaticPageAdminVM model = new StaticPageAdminVM()
            {
                Page = VMFactory.ŞtaticPageVMFactory(provider.Get(pageID)),
                Pages = provider.GetAll().Select(VMFactory.ŞtaticPageVMFactory).ToList()
            };
            return model;
        }

        public string GetPageContent(int pageID)
        {
            StaticPage page = provider.Get(pageID);
            return page.Content;
        }
    }
}
